﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Billing.Utilities;
using Billing.Dtos;
using System.Data;
using Billing.Controls;

namespace Billing.Windows
{
    /// <summary>
    /// Interaction logic for Expenditure.xaml
    /// </summary>
    public partial class Expenditure : Window
    {
        List<string> items = new List<string>();
        List<ExpenseItemControl> mainList;

        public Expenditure()
        {
            InitializeComponent();
            try
            {
                lbExpenditureItems.ItemsSource = Database.GetExpenseDetails();
                lbExpenditureItems.SelectedValuePath = "ID";
                lbExpenditureItems.DisplayMemberPath = "ExpenseItem";
                lbExpenditureItems.Focus();
                lbExpenditureItems.SelectedIndex = 0;

                mainList = new List<ExpenseItemControl>();
                foreach (DataRow row in Database.GetAllExpensesForToday().Rows)
                {
                    ExpenseItemControl item = new ExpenseItemControl();
                    item.lblExpense.Content = row[0].ToString();
                    item.lblAmount.Content = row[1].ToString();
                    item.lblDesc.Content = row[2].ToString();
                    item.lblCashier.Content = row[3].ToString();
                    mainList.Add(item);
                }

                lbExpensesGrid.ItemsSource = mainList;
            }
            catch (Exception) { }
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Escape)
                    this.Close();
                else if (e.Key == Key.F1)
                    SaveExpenditure();
            }
            catch (Exception) { }
        }

        private void lbExpenditureItems_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                    txtAmount.Focus();
            }
            catch (Exception) { }
        }

        private void txtAmount_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                    txtAdditionalDescription.Focus();
                else
                {
                    double result;
                    if (!(double.TryParse(txtAmount.Text.Trim(), out result)))
                        txtAmount.Clear();
                }
            }
            catch (Exception) { }
        }

        private void SaveExpenditure()
        {
            try
            {
                ExpenseDto expense = new ExpenseDto
                {
                    ExpenseID = ((Expense)(lbExpenditureItems as ListBox).SelectedItem).ID,
                    ExpenseName = ((Expense)(lbExpenditureItems as ListBox).SelectedItem).ExpenseItem,
                    Amount = Convert.ToDouble(txtAmount.Text.Trim()),
                    Description = txtAdditionalDescription.Text
                };

                if (Database.AddExpense(expense) > 0)
                {
                    MessageBox.Show("Expense Added Successfully.                        ", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
                    txtAmount.Clear();
                    txtAdditionalDescription.Clear();

                    mainList.Clear();
                    lbExpensesGrid.ItemsSource = null;

                    foreach (DataRow row in Database.GetAllExpensesForToday().Rows)
                    {
                        ExpenseItemControl item = new ExpenseItemControl();
                        item.lblExpense.Content = row[0].ToString();
                        item.lblAmount.Content = row[1].ToString();
                        item.lblDesc.Content = row[2].ToString();
                        item.lblCashier.Content = row[3].ToString();
                        mainList.Add(item);
                    }

                    lbExpensesGrid.ItemsSource = mainList;
                    lbExpensesGrid.SelectedIndex = -1;
                    lbExpenditureItems.Focus();

                    TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Previous);
                    UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
                    if (elementWithFocus != null)
                        elementWithFocus.MoveFocus(request);

                    lbExpenditureItems.SelectedIndex = 0;
                }
                else
                {
                    MessageBox.Show("Some Unknown Error has occurred.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            catch (Exception) { }
        }

        private void txtAdditionalDescription_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    lbExpensesGrid.Focus();
                    lbExpensesGrid.SelectedIndex = 0;
                }
            }
            catch (Exception) { }
        }

        private void lbExpensesGrid_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    lbExpenditureItems.Focus();
                    lbExpensesGrid.SelectedIndex = -1;
                }
            }
            catch (Exception) { }
        }
    }
}
